// 小程序开发api接口统一配置
const API_BASE_URL = "http://192.168.1.9:8081"; // 主域名
// const API_BASE_URL = "http://39.107.10.236:85/prod-api"; // 主域名
// const API_BASE_URL = 'http://jiance.app.wangh.top';  // 主域名
//const subdomain = "/app";

// const API_QINIU_URL = 'http://127.0.0.1:10031';
// const API_QINIU_URL = "http://192.168.1.96:10000";
//const API_QINIU_URL = "http://39.107.10.236:85/prod-api"; // 主域名
const request = (url, method, data) => {
	let _url = API_BASE_URL + url;
	let token = uni.getStorageSync("token");
	return new Promise((resolve, reject) => {
		uni.request({
			url: _url,
			method: method,
			data: data,
			header: {
				"Content-Type": "application/json",
				Authorization: "Bearer " + token,
			},
			success(request) {
				if (request.data.code == 401) {
					uni.showToast({
						title: "请重新登录",
						icon: "none",
						success() {
							setTimeout(() => {
								uni.navigateTo({
									// url: "/pages/login",
								});
							}, 2000);
						},
					});
				}
				resolve(request.data);
			},
			fail(error) {
				console.log(error);
				reject(error);
				uni.showToast({
					title: "请求超时",
					icon: "none",
				});
			},
			complete(aaa) {},
		});
	});
};
/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function(callback) {
	var Promise = this.constructor;
	return this.then(
		function(value) {
			Promise.resolve(callback()).then(function() {
				return value;
			});
		},
		function(reason) {
			Promise.resolve(callback()).then(function() {
				throw reason;
			});
		}
	);
};

export default {
	/* module.exports = { */
	request,
	//获取图片地址
	getPath() {
		// return API_QINIU_URL;
		return API_BASE_URL;
	},
	//获取请求地址
	getPathApi() {
		return API_BASE_URL;
	},

	//获取用户信息
	getUserInfo: data => request("/getInfo", "GET", data),
	//查询商品分类列表
	getGoodsClass: data => request("/lease/category/list", "get", data),
	//获取轮播图列表
	// getBannerList: data=> require("/lease/banner/list","get",data)
	getBannerList: data => request("/lease/banner/list", "get", data),
	//获取商品信息
	getList: data => request("/lease/good/list", "get", data),
	//查询首页标签商品
	getTatList: data => request("/lease/tags/getHomePageTag", "get", data),
	//获取商品详情
	getIdClass: (id) => request(`/lease/good/${id}`, "get"),
	//获取标签商品
	getTagArray: data => request('/lease/tags/list', "get", data),
	//商品收藏
	setCollection: data => request("/lease/collection", "post", data),
	//查看是否收藏
	getCollectionId: (id) => request(`/lease/collection/${id}`, "get"),
	//删除收藏
	deleteCollectin: (ids) => request(`/lease/collection/${ids}`, "DELETE"),
	//新增订单信息
	setOrder: data => request("/lease/order", "post", data),
	//查看订单信息
	getOrderList: data => request("/lease/order/list", "get", data)

}